﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.Text.RegularExpressions;

namespace QLCHHoaLan
{
    public partial class FrmQuanLyHoa : Form
    {
        public FrmQuanLyHoa()
        {
            InitializeComponent();
        }
        private void FrmQuanLyHoa_Load(object sender, EventArgs e)
        {
            HoaBUS hoaBUS = new HoaBUS();
            dgvHoa.DataSource = hoaBUS.LoadHoa();
            LoadDSNhaCungCap();
            LoadLoaiHoa();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (Validations.IsNullOrEmptyTextboxNull(txtTenHoa.Text) == true)
            {
                MessageBox.Show("Tên hoa hàng không hợp lệ!");
                return;
            }
            if (Validations.IsNullOrEmptyTextboxNull(txtXuatXu.Text) == true)
            {
                MessageBox.Show("Xuất xứ không hợp lệ!");
                return;
            }
            try
            {
                HoaDTO hoa = new HoaDTO();
                hoa.TenHoa = txtTenHoa.Text;
                hoa.XuatXu = txtXuatXu.Text;
                hoa.LoaiHoa = int.Parse(cbbLoaiHoa.SelectedValue.ToString());
                hoa.NhaCungCap = int.Parse(cbbNhaCungCap.SelectedValue.ToString());

                HoaBUS hoaBUS = new HoaBUS();
                hoaBUS.ThemHoa(hoa);
                MessageBox.Show("Thêm Thành Công");

                dgvHoa.DataSource = hoaBUS.LoadHoa();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (Validations.IsNullOrEmptyTextboxNull(txtTenHoa.Text) == true)
            {
                MessageBox.Show("Tên hoa hàng không hợp lệ!");
                return;
            }
            if (Validations.IsNullOrEmptyTextboxNull(txtXuatXu.Text) == true)
            {
                MessageBox.Show("Xuất xứ không hợp lệ!");
                return;
            }
            try
            {
                HoaDTO hoa = new HoaDTO();
                hoa.MaHoa = Int32.Parse(txtMaHoa.Text);
                hoa.TenHoa = txtTenHoa.Text;
                hoa.XuatXu = txtXuatXu.Text;
                hoa.LoaiHoa = Int32.Parse(cbbLoaiHoa.SelectedValue.ToString());
                hoa.NhaCungCap = Int32.Parse(cbbNhaCungCap.SelectedValue.ToString());

                HoaBUS hoaBUS = new HoaBUS();
                hoaBUS.CapNhatHoa(hoa);
                MessageBox.Show("Cập Nhật Thành Công");
                txtMaHoa.Text = null;
                txtTenHoa.Text = null;
                txtXuatXu.Text = null;
                dgvHoa.DataSource = hoaBUS.LoadHoa();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                HoaDTO hoa = new HoaDTO();
                hoa.MaHoa = Int32.Parse(txtMaHoa.Text);
                HoaBUS hoaBUS = new HoaBUS();
                hoaBUS.XoaHoa(hoa);
                txtMaHoa.Text = null;
                txtTenHoa.Text = null;
                txtXuatXu.Text = null;
                dgvHoa.DataSource = hoaBUS.LoadHoa();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message);
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            Menu main = new Menu();
            main.kq = 1;
            main.Show();
            this.Hide();
        }

        private void LoadLoaiHoa()
        {
            LoaiHoaBUS loaihoaBus = new LoaiHoaBUS();
            cbbLoaiHoa.DataSource = loaihoaBus.LayDSLoaiHoa();
            cbbLoaiHoa.DisplayMember = "TenLoai";
            cbbLoaiHoa.ValueMember = "MaLoai";
        }
        private void LoadDSNhaCungCap()
        {
            NhaCungCapBUS nccBUS = new NhaCungCapBUS();
            cbbNhaCungCap.DataSource = nccBUS.LayDSNhaCungCap();
            cbbNhaCungCap.DisplayMember = "TenNCC";
            cbbNhaCungCap.ValueMember = "MaNCC";
        }

        private void dgvHoa_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                txtMaHoa.Text = dgvHoa.Rows[e.RowIndex].Cells["MaHoa"].Value.ToString();
                txtTenHoa.Text = dgvHoa.Rows[e.RowIndex].Cells["TenHoa"].Value.ToString();
                txtXuatXu.Text = dgvHoa.Rows[e.RowIndex].Cells["XuatXu"].Value.ToString();
                cbbLoaiHoa.Text = dgvHoa.Rows[e.RowIndex].Cells["TenLoai"].Value.ToString();
                cbbNhaCungCap.Text = dgvHoa.Rows[e.RowIndex].Cells["TenNCC"].Value.ToString();
            }
            else
            {
                return;
            }
        }
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            txtMaHoa.Text = null;
            txtTenHoa.Text = null;
            txtXuatXu.Text = null;
        }  
        
    }
}
